package scu.maqiang.tiangong;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

import scu.maqiang.mesh.Mesh3T4;
import scu.maqiang.mesh.MeshFormat;
import scu.maqiang.numeric.MVO;

public class ReadZKEngineSurfaceData {

	public static void main(String[] args) {
		String dir = "TianGong\\ZKEngine\\";
		String meshFile = dir + "ZKEngine.mesh";
		Mesh3T4 mesh = new Mesh3T4().readMesh(meshFile, MeshFormat.mesh);
		System.out.println("Basic mesh information: ");
		System.out.println(mesh.getNv() + "\t" + mesh.getNt() + "\t" + mesh.getNb());
		//mesh.toTecplot(dir + "ZKEngineMesh.dat");
		HashSet<Integer> bdNodes = mesh.extractBoundaryNodes(0);

		int nv = mesh.getNv();
		double[] pressureData = new double[nv];
		double[] heatFluxData = new double[nv];
//		boolean[] flag = new boolean[nv];
		String dataFile = dir + "ZKEngine_PH.dat";
		int count = 0;
		Scanner scanner = null;

		try {
			scanner = new Scanner(new File(dataFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(scanner.next());
//		System.out.println(scanner.next());
//		System.out.println(scanner.next());
//		double min = 1.0e10;
//		int idx = -1;
//		while (scanner.hasNextDouble()) {
//			// 1. 依次读入每一行数据, 每一行前两个实数是坐标点x0, y0
//			// 2.将x0, y0与网格上的每一点进行比较, 看两者是否小于一个容许误差1.0e-10;
//			xyz0[0] = scanner.nextDouble();
//			xyz0[1] = scanner.nextDouble();
//			xyz0[2] = scanner.nextDouble();
//			xyz0[2] = -xyz0[2];
//			double pdata = scanner.nextDouble();
//			double hdata = scanner.nextDouble();
//			min = 1.0e10;
//			idx = -1;
//			for (int i = 0; i < nv; i++) {
//				double[] xyz = mesh.getNode(i);
//				xyz[2] = Math.abs(xyz[2]);
//				double err0 = Math.sqrt((xyz0[0] - xyz[0]) * (xyz0[0] - xyz[0])
//						+ (xyz0[1] - xyz[1]) * (xyz0[1] - xyz[1]) + (xyz0[2] - xyz[2]) * (xyz0[2] - xyz[2]));
//				// double err1 = Math.sqrt((xyz0[0] - xyz[0]) * (xyz0[0] - xyz[0])
//				// + (xyz0[1] - xyz[1]) * (xyz0[1] - xyz[1]) + (xyz0[2] + xyz[2]) * (xyz0[2] +
//				// xyz[2]));
//				double minval = MVO.min(err0);
//				if (!flag[i] && minval < 1.0e-6) {
//					flag[i] = true;
//					pressureData[i] = pdata;
//					heatFluxData[i] = hdata;
//					System.out.println("count: " + (++count));
//				}
//				if (min > minval) {
//					min = minval;
//					idx = i;
//				}
//			}
//			if(!flag[idx]) {
//				pressureData[idx] = pdata;
//				heatFluxData[idx] = hdata;
//				System.out.println("count: " + (++count));
//			}
//			
//		}
		
		ArrayList<double[]> data = new ArrayList<>();
		while(scanner.hasNextDouble()) {
			double xyz1 = scanner.nextDouble();
			double xyz2 = scanner.nextDouble();
			double xyz3 = scanner.nextDouble();
			double pres = scanner.nextDouble();
			double flux = scanner.nextDouble();
			data.add(new double[] {xyz1, xyz2, xyz3, pres, flux});
		}
		
		double min = 1.0e10;
		int idx = -1;
		boolean flag = false;
		for(int i = 0; i < nv; i++) {
			double[] xyz = mesh.getNode(i);
			xyz[2] = Math.abs(xyz[2]);
			min = 1.0e10;
			idx = -1;
			flag = false;
			for(int j = 0; j < data.size(); j++) {
				double[] d = data.get(j);
				d[2] = Math.abs(d[2]);
				double err0 = Math.sqrt((d[0] - xyz[0]) * (d[0] - xyz[0]) + (d[1] - xyz[1]) * (d[1] - xyz[1]) + (d[2] - xyz[2]) * (d[2] - xyz[2]));
				if (min > err0) {
					min = err0;
					idx = j;
				}
				if (err0 < 1.0e-6) {
					pressureData[i] = d[3];
					heatFluxData[i] = d[4];
					System.out.println("count: " + (++count));
					flag = true;
					break;
				}
			}
			if(!flag) {
				double[] dd = data.get(idx);
				pressureData[i] = dd[3];
				heatFluxData[i] = dd[4];
				System.out.println("count: " + (++count));
			}
		}
		System.out.println("Total Nodes:" + mesh.getNv());
		System.out.println("Surface Nodes: " + bdNodes.size());
		scanner.close();
		mesh.toTecplot(dir + "PressureData0.dat", pressureData);
		mesh.toTecplot(dir + "heatData0.dat", heatFluxData);
		MVO.saveArray(dir + "ZKEngine_Pressure0.dat", pressureData);
		MVO.saveArray(dir + "ZKEngine_Heatflux0.dat", heatFluxData);
	}
}
